package com.xiaolin.schedule.config;

import com.xiaolin.schedule.quartz.MyScheduleTask;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName QuartzConfig
 * @Description TODO
 * @Detail detail
 * @Author MyPC
 * @Date 2020/9/27
 * @Version 1.0
 */
@Configuration
public class QuartzConfig {

    /**
     * 配置任务
     * @return
     */
    @Bean
    public JobDetail uploadTaskDetail(){
        //指定具体任务、任务名
        return JobBuilder.newJob(MyScheduleTask.class).withIdentity("mytask").storeDurably().build();
    }

    /**
     * 配置定时触发器
     * @return
     */
    @Bean
    public Trigger uploadTrigger(){
        //任务调度构建器，创建执行任务的时间规则
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
        //指定 JobDetail ，任务名、任务调度构建器
        return TriggerBuilder.newTrigger().forJob(uploadTaskDetail())
                .withIdentity("mytask")
                .withSchedule(scheduleBuilder)
                .build();
    }
}
